package com.spider.demo4test;

import java.util.*;

public class Room {
    //1.准备号54张牌，给房间使用，定义一个集合，保存54张牌
    private List<Card> roomCards = new ArrayList<>();
    //2.初始化54张牌进去。
    {
        //3.准备点数
        String[]nums = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //4.准备花色
        String[]colors = {"♠","♥","♣","♦"};
        //5.开始创建52张牌
        for (String num : nums) {
            for (String color : colors) {
                Card card = new Card(num,color);
                roomCards.add(card);
            }
        }
        //6.创建大小王
        roomCards.add(new Card("小王","🤡"));
        roomCards.add(new Card("大王","🤡"));
        System.out.println("洗牌前："+roomCards);
    }

    public void start() {
        //7.洗牌
        Collections.shuffle(roomCards);
        System.out.println("洗牌后："+roomCards);
        //8.开始发牌
        Map<String, List<Card>> players = new LinkedHashMap<>();
        players.put("玩家1",new ArrayList<>());
        players.put("玩家2",new ArrayList<>());
        players.put("玩家3",new ArrayList<>());
        //发前51张牌
        for (int i = 0; i < roomCards.size()-3; i++) {
            Card card = roomCards.get(i);
            if (i%3==0)
                players.get("玩家1").add(card);
            else if (i%3==1)
                players.get("玩家2").add(card);
            else
                players.get("玩家3").add(card);
        }
        //9.对牌排序（先建立点数与花色的顺序映射）
        final Map<String, Integer> rankOrder = new HashMap<>();
        // 斗地主常用点数顺序：3 < 4 < ... < K < A < 2 < 小王 < 大王
        List<String> ranks = Arrays.asList(
                "3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"
        );
        for (int i = 0; i < ranks.size(); i++) {
            rankOrder.put(ranks.get(i), i);
        }

        final Map<String, Integer> suitOrder = new HashMap<>();
        // 花色优先级（你可以按需调整）：♠ > ♥ > ♣ > ♦
        suitOrder.put("♠", 3);
        suitOrder.put("♥", 2);
        suitOrder.put("♣", 1);
        suitOrder.put("♦", 0);
        // 若 Card 的花色用其它字符串（或 emoji），请相应调整映射键

        // 针对每位玩家（包含底牌）进行排序
        players.forEach((name, cards) -> {
            Collections.sort(cards, new Comparator<Card>() {
                @Override
                public int compare(Card o1, Card o2) {
                    // 先获取点数映射值（若不存在，给一个很小/很大值以防 NPE）
                    Integer r1 = rankOrder.get(o1.getSize());
                    Integer r2 = rankOrder.get(o2.getSize());
                    if (r1 == null) r1 = -1;
                    if (r2 == null) r2 = -1;
                    int rankCmp = r1.compareTo(r2);
                    if (rankCmp != 0) return rankCmp;

                    // 点数相同（通常只发生在普通牌），再比较花色
                    Integer s1 = suitOrder.get(o1.getColor());
                    Integer s2 = suitOrder.get(o2.getColor());
                    if (s1 == null) s1 = -1;
                    if (s2 == null) s2 = -1;
                    return s1.compareTo(s2);
                }
            });
        });
        //获取三张底牌
        List<Card> dipai = roomCards.subList(roomCards.size()-3,roomCards.size());
        players.put("底牌",dipai);
        //10.开始看牌
        for (Map.Entry<String, List<Card>> entry : players.entrySet()) {
            //获取玩家名称
            String name = entry.getKey();
            //获取玩家牌
            List<Card> cards = entry.getValue();
            System.out.println(name+"："+cards);
        }
    }
}
